# -*- coding: utf-8 -*-
'''
Created on 2019/11/19 8:41 
author:dyx
IDE:PyCharm 
'''
#向文本文件中写入内容
'''
f = open('Sample.txt','a+')
s = '文本文件的读取方法\n文本文件的写入方法\n'
f.write(s)
f.close()
s1 = '我是第二次修改'
with open('Sample.txt','a+') as f:
    f.write(s1)
'''
#读取并显示文本文件的前5个字节
'''
fp = open('Sample.txt','r')
# print(fp.read(5))
print(fp.read(20))
fp.seek(0)
print(fp.read(20))
fp.close()
'''
#读取并显示文本文件的所有行
'''
f = open('Sample.txt','r')
while True:
    line = f.readline()
    if line == '':
        break
    print(line,end='')
'''
'''
#第二种写法
f = open('sample.txt','r')
li = f.readlines()
for line in li:
    print(line,end='')
f.close()
'''
#使用pickle模块写入二进制文件
import pickle
f = open('sample_pickle.dat','wb')
n = 7
i = 1400000
a = 99.056
s = '中国人民123abc'
lst = [[1,2,3],[4,5,6],[7,8,9]]
tu = (-5,10,8)
coll = {4,5,6}
dic = {'a':'apple','b':'banana','g':'grape','o':'orange'}
try:
    pickle.dump(n,f)
    pickle.dump(i,f)
    pickle.dumpe(a,f)
    pickle.dump(s,f)
    pickle.dump(lst,f)
    pickle.dump(tu,f)
    pickle.dump(coll,f)
    pickle.dump(dic,f)
except:
    print('写文件异常')
finally:
    f.close()
